<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/6 0006
 * Time: 14:09
 */
include 'sql.php';
$mysql = new mysql;

//留言内容查询
//$sql = "select * from message where sn = 0 and p_id = ".$_SESSION['id'].' or g_id = '.$_SESSION['id'];
$sql = 'select * from message where p_id = '.$_SESSION["id"].' or g_id = '.$_SESSION["id"].' and sn = 0 order by time desc;';
$data = $mysql->select($sql);
//重组数组，将发送人和接收人装入
foreach($data as $i => $j){
    $aql1 = "select username from test_table where id = ".$j['p_id'];
    $user1 = $mysql->onedata($aql1);
    $data[$i]['put_username'] = $user1['username'];
    $aql2 = "select username from test_table where id = ".$j['g_id'];
    $user2 = $mysql->onedata($aql2);
    $data[$i]['get_username'] = $user2['username'];
}

//提交新留言
if (isset($_REQUEST['insert'])) {
    if (!empty($_POST)) {
        $text = $_POST['text'];
        $p_id = $_SESSION['id'];
        $g_id = $_POST['g_id'];
        $time = date('Y-m-d H:i:s');
        $int = "insert into message (p_id,g_id,text,time) VALUES ($p_id,$g_id,'" . $text . "','" . $time . "');";
        $ints = $mysql->insert($int);
        if ($ints == true) {
            alert("发送成功", 0, 'message.php');
        } else {
            alert("发送失败",0,'message.php');
        }
    }
}

//删除
if (isset($_REQUEST['delete'])) {
    if (!empty($_POST)) {
        $id = $_POST['id'];
        $int = "DELETE FROM `test`.`message` WHERE  `id` = $id;";
        $ints = $mysql->delete($int);
        if ($ints == true) {
            alert("删除成功。", 0, 'message.php');
        } else {
            alert("删除失败。", 0, 'message.php');
        }
    }
}
//修改
/*if (isset($_REQUEST['update'])) {
    if (!empty($_POST)) {
        $id = $_POST['id'];
        $text = $_POST['text'];
        $int = "UPDATE message SET text = '".$text."' WHERE  id = $id;";
        $ints = $mysql->update($int);
        if ($ints == true) {
            alert("修改成功。", 0, 'message.php');
        } else {
            alert("修改失败。", 0, 'message.php');
        }
    }
}*/
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <script type="text/javascript" charset="utf-8" src="ed/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="ed/ueditor.all.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="ed/lang/zh-cn/zh-cn.js"></script>
    <title>留言板</title>
    <style type="text/css">
        *{
            padding: 0;
            margin: 0;
        }
        #center div{
            width:100%;
        }
        #head{
            width: 100%;
            height: 50px;
        }
        #head_son{
            margin: 0 auto;
            height: 50px;
            text-align: center;
        }
        #head_son span{
            font-size: 40px;
            color: deepskyblue;
        }
        #left{
            float: left;
            width: 443px;
            margin: 20px 50px;
        }
        #left input{
            width: 50px;
            height: 25px;
        }
        #box_left{
            padding: 10px 0 0 10px;
            width: 210px;
            height: 180px;
            float: left;
            border: 1px solid cyan;
        }
        #box_right{
            padding: 10px 0 0 10px;
            width: 210px;
            height: 180px;
            float: right;
            border: 1px solid cyan;
        }
        #center{

        }
        #right{
            float: right;
        }
        #right button{
            width: 100px;
            height: 30px;
        }
        #editor{
            width: 900px;
            height: 500px;
        }
    </style>
</head>
<body>
    <div id="head">
        <div id="head_son">
            <span>欢迎：
                <?php
                echo $_SESSION["username"];
                ?>
            </span>
        </div>
    </div>

    <div id="left">
        <h1>留言内容</h1>
        <?php
        foreach($data as $key => $value){
            if($_SESSION["id"] == $value['p_id']){  ?>
            <div id="box_right">
            <?php   }else{  ?>
            <div id="box_left">
            <?php    };  ?>
                <form method="post">
                    <span>发送人：</span><?php echo $value['put_username']; ?><br>
                    <span>接收人：</span><?php echo $value['get_username']; ?><br>
                    <div style="width: 400px;color: black"><span>时间：</span><?php echo $value['time']; ?></div>
                    <span>内容：</span><br>
                    <textarea style="width: 200px;height: 60px;" id="txt" name="text"><?php echo $value['text']; ?></textarea>
                    <input type="hidden" name="key" value="<?php echo $key; ?>">
                    <input type="hidden" name="id" value="<?php echo $value['id']; ?>"><br>
                    <input type="submit" value="删除" name="delete" onclick="return on_sub1()">
                    <?php   if($_SESSION["id"] == $value['p_id']){  ?>
                        <input type="submit" value="修改" name="update" onclick="return on_sub2()">
                    <?php   }
                    //修改
                    if (isset($_REQUEST['update'])) {
                        if (!empty($_POST)) {
                            $id = (int)$_POST['id'];
                            $p_key = (int)$_POST['key'];
                            $sqltxt = $data["$p_key"]['text'];
                            $text = $_POST['text'];
                            if ($text == '' || $text == NULL){
                                alert("内容不能为空！",0,"message.php");
                            }else{
                                if($sqltxt == $text){
                                    alert("你还没有修改",0,"message.php");
                                }else{
                                    $int = "UPDATE message SET text = '".$text."' WHERE  id = $id;";
                                    $ints = $mysql->update($int);
                                }
                                if ($ints == true) {
                                    alert("修改成功。", 0, 'message.php');
                                } else {
                                    alert("修改失败。", 0, 'message.php');
                                }
                            }
                        }
                    }
                    ?>
                </form>
            </div>
        <?php    }  ?>
    </div>

    <div>
        <h1>发送留言</h1>
        <!--查询出的全部用户，除去自己和冻结-->
        <form action="test-ed.php" method="post">
            <div style="width:100%;">
                <script id="editor" type="text/plain"></script>
            </div>
            <select name="g_id" id="">
                <?php
                //查询全部用户
                $sql2 = "select * from test_table where dd = 0";
                $user =  $mysql->select($sql2);
                foreach($user as $m => $n){
                    if($n['id'] != $_SESSION['id']){    ?>
                        <option value="<?php echo $n['id']  ?>">
                            <?php echo $n['username']   ?>
                        </option>
                    <?php };?>
                <?php };?>
            </select>
            <input type="submit" value="发送" name="insert" onclick="return on_form()">
        </form>
    </div>

    <div id="right">
        <button onclick="btn()">退出登录</button>
    </div>



<script type="text/javascript">
    //实例化编辑器
    //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
    var ue = UE.getEditor('editor');

    function on_sub1() {
        var mymessage=confirm("确定要删除吗？");
        if(mymessage == true){
            window.location.href="message.php";
            return true;
        }else {
            window.location.href="message.php";
            return false;
        }
    }
    function on_sub2() {
        var mymessage=confirm("确定要修改吗？");
        if(mymessage == true){
            window.location.href="message.php";
            return true;
        }else {
            window.location.href="message.php";
            return false;
        }
    }
    function on_form() {
        let text = document.getElementById("editor").value;
        if(text == '' || text == null){
            alert("发送内容不能为空。");
            return false;
        }
    }
    function btn() {
        var mymessage=confirm("确定要退出登录吗？");
        if(mymessage == true){
            window.location.href="login.php";
            return true;
        }else {
            window.location.href="message.php";
            return false;
        }
    }


</script>
</body>
</html>